package com.example.zkWebClient.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.zkWebClient.common.AjaxCommonResult;
import com.example.zkWebClient.domain.TreeNode;
import com.example.zkWebClient.domain.ZNode;
import com.example.zkWebClient.service.NodeService;

@Controller
@RequestMapping("/asyncTreeNode")
public class AsyncTreeNodeController {

	private static final Logger LOGGER = LoggerFactory.getLogger(AsyncTreeNodeController.class);

	@Autowired
	private NodeService nodeService;

	@GetMapping("/main")
	public String main() {
		return "async";
	}

	/**
	 * 获取子节点列表
	 * @param path
	 * @return
	 * @author: jie.deng
	 * @time: 2017年12月27日 下午3:42:45
	 */
	@GetMapping("/findChildNodes")
	@ResponseBody
	public Object findChildNodes(String path) {
		List<TreeNode> nodeList = new ArrayList<>();

		List<String> childPathList = nodeService.findChildNodes(path);
		if (childPathList != null && childPathList.size() > 0) {
			Collections.sort(childPathList);
			for (String childPath : childPathList) {
				String absolutePath = "/".equals(path) ? (path + childPath) : (path + "/" + childPath);
				ZNode node = nodeService.getNode(absolutePath);
				if (node != null) {
					nodeList.add(new TreeNode(node));
				}
			}
		}
		return nodeList;
	}

	@PostMapping("/addNode")
	@ResponseBody
	public Object addNode(ZNode node) {
		try {
			String pathCreated = nodeService.addNode(node);
			TreeNode treeNode = new TreeNode();
			treeNode.setName(pathCreated.substring(pathCreated.lastIndexOf("/") + 1));
			treeNode.setPath(pathCreated);
			treeNode.setIsParent("false");
			treeNode.setData(node.getData());
			return AjaxCommonResult.success(treeNode);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
			return AjaxCommonResult.fail();
		}
	}

	@PutMapping("/updateNode")
	@ResponseBody
	public Object updateNode(ZNode node) {
		try {
			nodeService.updateNode(node);
			return AjaxCommonResult.success(Boolean.TRUE);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
			return AjaxCommonResult.fail();
		}
	}

	@DeleteMapping("/delNode")
	@ResponseBody
	public Object delNode(String path) {
		try {
			nodeService.delNode(path);
			return AjaxCommonResult.success(Boolean.TRUE);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
			return AjaxCommonResult.fail();
		}
	}

}
